package com.dream.service.impl;

import java.util.List; 

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dream.able.FriendshipLinkMapper;
import com.dream.datasource.DBContextHolder;
import com.dream.pojo.FriendshipLink;
import com.dream.pojo.FriendshipLinkExample;
import com.dream.service.able.All_Service_able;
import com.dream.service.able.Limit_able;


/*
 * 友情链接的业务类
 */
@Service("Link_Serviceimpl")
public class Link_Serviceimpl implements 
All_Service_able<FriendshipLink> ,
Limit_able<FriendshipLink>{

	/*
	 * 友情链接表的DAO代理对象
	 */
	@Resource(name="friendshipLinkMapper")
	private FriendshipLinkMapper friendship_link_mapper;

	/*
	 * (添加友情链接)
	 * @see com.dream.serviceable.Link_Serviceable#insertLink(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public FriendshipLink insert(String ... str) {

		//切换数据源，主库
		DBContextHolder.setDbType(DBContextHolder.master);
		//创建实体类
		FriendshipLink friendship_link = new FriendshipLink();
		//插入标题
		friendship_link.setTitle(str[0]);
		//插入友情链接的logo地址
		friendship_link.setLinkImg(str[1]);
		//插入友情链接的地址
		friendship_link.setLinkHttp(str[2]);
		//插入域名
		friendship_link.setDomainName(str[3]);
		//插入状态
		friendship_link.setState(0);
		//写入数据库
		return friendship_link_mapper.insertSelective(friendship_link)>0?friendship_link:null;
	}

	/*
	 * (查询所有state=1的友情链接)
	 * @see com.dream.serviceable.Link_Serviceable#listLink()
	 */
	@Override
	public List<FriendshipLink> list() {
		//切换数据源，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		FriendshipLinkExample friendship_link_example=new FriendshipLinkExample();
		//创建查询条件工具内部类
		FriendshipLinkExample.Criteria criteria= friendship_link_example.createCriteria();
		//设置查询条件
		criteria.andStateEqualTo(1);
		//返回查询到的集合
		return friendship_link_mapper.selectByExample(friendship_link_example);
	}

	@Override
	public FriendshipLink getT(String... str) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (查询指定条数和位置的数据,传入开始的位置，和返回的数据数目)
	 * @see com.dream.service.able.Limit_able#list_Limit(java.lang.String[])
	 */
	@Override
	public List<FriendshipLink> list_Limit(String... str) {
		//切换数据源，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		FriendshipLinkExample friendship_link_example=new FriendshipLinkExample();
		//设置查询的开始位置
		friendship_link_example.setOffset(Integer.parseInt(str[0]));
		//设置返回的数目
		friendship_link_example.setLimit(Integer.parseInt(str[1]));
		//创建查询条件工具内部类
		FriendshipLinkExample.Criteria criteria= friendship_link_example.createCriteria();
		//设置查询条件
		criteria.andStateEqualTo(1);
		//返回查询到的集合
		return friendship_link_mapper.selectByExample(friendship_link_example);
	}
}
